﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChuanYe.CountyFireManagement.Api.Models;
using ChuanYe.CountyFireManagement.EF.MySQL.Models;
using ChuanYe.CountyFireManagement.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ChuanYe.CountyFireManagement.Api.Controllers
{
    /// <summary>
    ///  火灾驻守
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FireStationController : ControllerBase
    {

        /// <summary>
        ///  增加
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Add(FireStationRequest obj)
        {
            string ip = HttpContext.GetClientIp();
            ResultDtoBase result = new ResultDtoBase();
            Firestation firestation = new Firestation();
            firestation.FireId = obj.FireId;
            firestation.StationContent = obj.StationContent;
            firestation.SendMode = obj.SendMode;
            firestation.SceneUserId = obj.SceneUserId;
            firestation.EstimateLatitude = obj.EstimateLatitude;
            firestation.EstimateLongitude = obj.EstimateLongitude;
            firestation.EstimateAltitude = obj.EstimateAltitude;
            firestation.UserId = obj.UserId;
            firestation.Ip = ip;
            firestation.TransDatetime = obj.TransDatetime.HasValue ? obj.TransDatetime.Value : DateTime.Now;

            Firesite firesite = new Firesite();
            firesite.FireId = obj.FireId;
            firesite.Latitude = obj.EstimateLatitude;
            firesite.Longitude = obj.EstimateLongitude;
            firesite.Altitude = obj.EstimateAltitude;
            firesite.UserId = obj.UserId;
            firesite.TransDatetime = firestation.TransDatetime;
            firesite.Ip = firestation.Ip;
            try
            {
                var data = BLL.FireStationBLL.Instance.Add(firestation,firesite);
                if (data.FirstParam != null)
                {
                    ControllerHelper.SendMessage(data.FirstParam);
                    var allExpireFreInfo = BLL.FireExpireBLL.Instance.GetUnExpire();
                    MqttHub.fireexpires = allExpireFreInfo;
                }
                result.Status = data.Status;
                result.Message = data.Message;
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/FireStation/Add Server Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }



        /// <summary>
        ///  完成 火灾驻守
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Complete(FireCompleteStationRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                if (obj.FireId == 0 || obj.SceneUserId == 0)
                {
                    result.Status = 0;
                    result.Message = "FireId Or SceneUserId Equals 0";
                }
                else
                {
                    string ip = HttpContext.GetClientIp();
                    List<Firestationattach> firestationattachs = new List<Firestationattach>();
                    if (obj.Attachs != null)
                    {
                        foreach (var item in obj.Attachs)
                        {
                            Firestationattach firestationattach = new Firestationattach();
                            firestationattach.Direction = item.Direction;
                            firestationattach.ShotLatitude = item.ShotLatitude;
                            firestationattach.ShotLongitude = item.ShotLongitude;
                            firestationattach.ShotAltitude = item.ShotAltitude;
                            firestationattach.SceneFile = item.SceneFile;
                            firestationattachs.Add(firestationattach);
                        }
                    }
                    var data = BLL.FireStationBLL.Instance.Complete(obj.FireId, obj.SceneUserId,obj.IsReburn, obj.SceneTime, ip, 
                                                                     obj.ReviseLatitude, obj.ReviseLongitude, obj.ReviseAltitude, firestationattachs);
                    if (data.FirstParam != null)
                    {
                        ControllerHelper.SendMessage(data.FirstParam);
                        var allExpireFreInfo = BLL.FireExpireBLL.Instance.GetUnExpire();
                        MqttHub.fireexpires = allExpireFreInfo;
                    }
                    result.Status = data.Status;
                    result.Message = data.Message;
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/FireStation/Complete Server Error";
                LogHelper.Error(result.Message,ex);
            }
            return result;
        }


    }
}